package sz.szsmk.citizencard.ui.dzsmkavtivity;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.gyf.immersionbar.ImmersionBar;

import java.text.ParseException;

import javax.inject.Inject;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.AdvertisementsRes;
import sz.szsmk.citizencard.model.network.dzbean.res.GetUserInfoRes;
import sz.szsmk.citizencard.model.network.dzbean.res.MessageEvent;
import sz.szsmk.citizencard.mvp.BasePresenter;
import sz.szsmk.citizencard.mvp.PersonInfoQueryMvpView;
import sz.szsmk.citizencard.mvp.dzsmkpresenter.RealPresenter;
import sz.szsmk.citizencard.util.IDCardUtil;
import sz.szsmk.citizencard.util.PubUtils;

/**
 * Created by yangzhengwen on 2017/10/1.
 */

public class RealNameAuthenticationActivity extends AppBaseActivity implements PersonInfoQueryMvpView {

    EditText et_qr_name;
    EditText et_qr_id;
    TextView tv_qr_next;

    private boolean isMain;

    @Inject
    RealPresenter realPresenter;

    public static final String ZH_REG = "[\\u4e00-\\u9fa5]+";

    @Override
    protected BasePresenter[] initPresenters() {
        return new BasePresenter[]{realPresenter};
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getComponent().inject(this);
        setContentView(R.layout.qr_activity_real_name_authentication);

        et_qr_name = findViewById(R.id.et_qr_name);
        et_qr_id= findViewById(R.id.et_qr_id);
        tv_qr_next= findViewById(R.id.tv_qr_next);
        tv_qr_next.setOnClickListener(this::onClickView);

        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true, 0f)
                .statusBarColor(R.color.white).init();
//        String status = SecureSharedPreferences.getString(Constant.QUERY_QR_STATUS);
        String isRealName = getIntent().getStringExtra("isRealName");
        isMain = getIntent().getBooleanExtra("MainFragment", false);
        if (isRealName.equals("0")) {
            tv_qr_next.setVisibility(View.VISIBLE);
        } else {
            realPresenter.getPersonInfo();
            tv_qr_next.setVisibility(View.GONE);
            et_qr_name.setEnabled(false);
            et_qr_id.setEnabled(false);
        }
    }

    @Override
    protected CharSequence getTopTitle() {
        return "实名认证";
    }

    public void onClickView(View v) {
        if (v.getId() == R.id.tv_qr_next) {
            if (check()) {
//                Intent intent = new Intent(RealNameAuthenticationActivity.this, BindBankCardActivity.class);
//                intent.putExtra("type", 0);
//                intent.putExtra("name", et_qr_name.getText().toString());
//                intent.putExtra("iccard", et_qr_id.getText().toString());
//                startActivityForResult(intent, 123);
                Intent intent = new Intent(RealNameAuthenticationActivity.this, AppendRealNameInfoActivity.class);
                intent.putExtra("type", 0);
                intent.putExtra("name", et_qr_name.getText().toString());
                intent.putExtra("iccard", et_qr_id.getText().toString());
                startActivityForResult(intent, 123);
            }
        }
    }

    @Override
    public void onGainPersonInfo(GetUserInfoRes user) {
        String realName = user.getData().getRealName();
        if (!TextUtils.isEmpty(realName)) {
            String preRealName = realName.substring(0, 1);
            et_qr_name.setText(preRealName + "**");
        }
        String idCardNumber = user.getData().getIdCardNumber();
        if (!TextUtils.isEmpty(idCardNumber) && idCardNumber.length() > 4) {
            String preIdcard = idCardNumber.substring(0, 4);
            et_qr_id.setText(preIdcard + "***");
        }

    }

    @Override
    public void getAdvertisementsResult(AdvertisementsRes advertisementsRes) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 123 && resultCode == 456) {
            if (isMain) {
                eventBus.post(new MessageEvent(MessageEvent.CODE_REFRESH));
            } else {
                eventBus.post(new MessageEvent(MessageEvent.CODE_UPDATE_USERINFO_SUCCESS));
            }
            finish();
        }
    }

    private boolean check() {
        if (TextUtils.isEmpty(et_qr_name.getText().toString())) {
            showTipDialog("姓名不能为空");
            return false;
        } else if (!et_qr_name.getText().toString().matches(ZH_REG)) {
            showTipDialog("姓名只能是中文,不能包含特殊字符");
            return false;
        } else if (TextUtils.isEmpty(et_qr_id.getText().toString())) {
            showTipDialog("身份证不能为空");
            return false;
        } else if (et_qr_id.getText().toString().length() < 18) {
            showTipDialog("身份证位数不正确");
            return false;
        }
        try {
            if (!TextUtils.isEmpty(IDCardUtil.IDCardValidate(et_qr_id.getText().toString()))) {
                showTipDialog("请输入正确的身份证号");
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return true;
    }

    private void showTipDialog(String tip) {
        PubUtils.popTipOrWarn(RealNameAuthenticationActivity.this, tip);
    }
}
